import 'package:flutter/material.dart';
import 'package:flutter/scheduler.dart';
import 'pages/root/main_page.dart';

void main() => runApp(const MyApp());
GlobalKey<NavigatorState> navigatorKey = GlobalKey<NavigatorState>();

class MyApp extends StatefulWidget {
  const MyApp({super.key});

  @override
  State<MyApp> createState() => _MyAppState();
}

// WidgetsBindingObserver：Widget观察器
class _MyAppState extends State<MyApp> with WidgetsBindingObserver {
  Brightness brightness = Brightness.light;
  @override
  void initState() {
    super.initState();
    setTheme();
    // 添加观察器
    WidgetsBinding.instance.addObserver(this);
  }

  @override
  // didChangeAppLifecycleState：获取app状态
  void didChangeAppLifecycleState(AppLifecycleState state) {
    // TODO: implement didChangeAppLifecycleState
    super.didChangeAppLifecycleState(state);
    // 回到APP
    if (state == AppLifecycleState.resumed) {
      setTheme();
    }
    print(state);
  }

  void setTheme() {
    Brightness brightness = SchedulerBinding
        .instance.platformDispatcher.platformBrightness; //获取系统主题
  }

  @override
  void dispose() {
    super.dispose();
    // 页面销毁时移除观察器
    WidgetsBinding.instance.removeObserver(this);
  }

  @override
  Widget build(BuildContext context) {
    return MaterialApp(
      debugShowCheckedModeBanner: false,
      title: 'Material App',
      // splashColor：水波纹效果，由内向外发散；highlightColor：高亮效果
      theme: ThemeData(
          splashColor: Colors.transparent,
          highlightColor: Colors.transparent,
          brightness: brightness),
      home: const MainPage(),
      // navigatorKey 相当于 Navigator.of(context) ，如果应用程序想实现无 context 跳转，那么可以通过设置该key, 通过 navigatorKey.currentState!.context 获取全局context
      navigatorKey: navigatorKey,
    );
  }
}
